var app = getApp();
var api = require('../../api.js');
var tabbarTemplate = require("../tab-bar/tab-bar.js") 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    colorArr: ["#24c4f8", "#fd78a6", "#ffb174", "#ed65eb", "#2fe2e0", "#4adb51","#35d9a2"
      ],
    // 存储随机颜色
    randomColorArr: [],

    examTable: [
      {
        link:"../test-paper/test-paper",
        xuhao:"1",
        title: "法的概念和特征",
        version: "备战高考，查漏补缺"
      },
      {
        link: "../test-paper/test-paper",
        xuhao: "2",
        title: "历年高考卷",
        version: "备战高考，查漏补缺"
      },
      {
        link: "../test-paper/test-paper",
        xuhao: "3",
        title: "驾照考题",
        version: "备战高考，查漏补缺"
      }
    ],
    course_list:[],
    
    subject_id:0,
    index_data:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this,
      labLen = that.data.examTable.length,
      colorArr = that.data.colorArr,
      colorLen = colorArr.length,
      randomColorArr = [];
    //判断执行
    do {
      let random = colorArr[Math.floor(Math.random() * colorLen)];
      randomColorArr.push(random);
      labLen--;
    } while (labLen > 0)

    that.setData({
      randomColorArr: randomColorArr
    });
    tabbarTemplate.tabbar("tabBar", 0, this, 'tiku')
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    if (this.data.subject_id==0){
      this.setData({
        subject_id: wx.getStorageSync('subject_id')
      })
    }
    this.getIndex()
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //科目下课程列表
  getCourseList: function () {
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.paper.subject_course,
      method: 'POST',
      data: {
        subject_id:page.data.subject_id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            course_list: res.data,
          })
          page.getIndexData()
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //首页信息
  getIndex:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.paper.paper_index,
      method: 'POST',
      data: {
      },
      success: function (res) {
        console.log(res)
        if (res.errcode == 0) {
          page.setData({
            index_data: res.data,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //tabbar跳转
  tabBarRedirect: function (e) {
    wx.redirectTo({
      url: e.currentTarget.dataset.url,
    })
  }
})